package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.Model;
import com.lsyw.intergrate.entity.Monitoring;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 模型元素 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-29
 */
@Api(tags = "模型元素")
@RestController
@RequestMapping("/model")
public class ModelController {


    @Autowired
    private ModelService modelService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    @ApiOperation(value = "通过id查询", notes = "通过id查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result getById(String id) {
        return Result.successResult(modelService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST")
    public Result save(@RequestBody Model model) {
        modelService.save(model);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id")
    @ApiOperation(value = "通过id删除", notes = "通过id删除", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    public Result delete(String id) {
        Model model = this.modelService.getById(Long.parseLong(id));
        model.setDelFlag(1);
        this.modelService.updateById(model);
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
   // @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST")
    public Result updateById(@RequestBody Model model) {
        model.setDelFlag(0);
        model.setGmtModified(new Date());
        modelService.updateById(model);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询列表", notes = "查询列表", httpMethod = "POST")
    public Result list(@RequestBody ModelReqVo model) {
        final LambdaQueryWrapper<Model> lambda = new QueryWrapper<Model>().lambda();
        this.buildCondition(lambda, model);
        return Result.successResult(modelService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询列表", notes = "查询列表", httpMethod = "POST")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<Model> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<Model> lambda = new QueryWrapper<Model>().lambda();
        lambda.eq(Model::getDelFlag,0);
        Map<String, Object> condition = pageInfo.getCondition();
        if (condition.get("name") != null && !StringUtils.isEmpty(condition.get("name").toString())) {
            lambda.like(Model::getName, condition.get("name"));
        }
        if (condition.get("type") != null && !StringUtils.isEmpty(condition.get("type").toString())) {
            lambda.eq(Model::getType, condition.get("type"));
        }
        return Result.successResult(this.modelService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<Model> lambda, ModelReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(Model::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(Model::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getFileUrl())) {
            lambda.eq(Model::getFileUrl, param.getFileUrl());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(Model::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(Model::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(Model::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(Model::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(Model::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(Model::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(Model::getDelFlag, param.getDelFlag());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(Model::getMassifId, param.getMassifId());
        }
        if (!StringUtils.isEmpty(param.getType())) {
            lambda.eq(Model::getType, param.getType());
        }
        if (!StringUtils.isEmpty(param.getIcon())) {
            lambda.eq(Model::getIcon, param.getIcon());
        }
        lambda.orderBy(true, false, Model::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class ModelReqVo extends Model {
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
